/**
 * 表单设计器
 */
mini.parse();

var mainField = document.querySelector('#mainField');//主面板

var cFormName = mini.get("cFormName");//英文
var cTableField = mini.get("cTableField");//英文
var cLadText = mini.get("cLadText");//中文
var cTabText = mini.get("cTabText");//页签名
var cLength = mini.get("cLength");//长度
var cFormat = mini.get("cFormat");//日期格式
var cReference = mini.get("cReference");//参照
var cBgReference = mini.get("cBgReference");//参照2
var cCombo = mini.get("cCombo");//下拉框内容
var cGridType = mini.get("cGridType");//表格列类型
var cMath = mini.get("cMath");//公式

var textbox_index = 0;
var tab_index = 0;
var table_index = 0;
var checkbox_index = 0;
var checkboxlist_index = 0;
var combobox_index = 0;
var buttonedit_index = 0;
var datepicker_index = 0;
var textarea_index = 0;

var textbox_editIndex = [];
var tab_editIndex = [];
var table_editIndex = [];
var checkbox_editIndex = [];
var checkboxlist_editIndex = [];
var combobox_editIndex = [];
var buttonedit_editIndex = [];
var datepicker_editIndex = [];
var textarea_editIndex = [];

var tabsId_arr = [];
var tab_index = 1;

var rightClickCol = {}; //表格右键点击选中的列对象
var properties = [];

var action;
var edit_data;
var formListId;
var formName;

var menuJump = parent.isMenuJump();//是否是菜单跳转（add）
if(menuJump) {//菜单跳转（新增）
    action = 'add';
    edit_data = [];
    formListId = '';
    formName = '';

}else {//按钮跳转（新增/编辑）

    var formDesignAction = localStorage.getItem("formDesignAction");
    formDesignAction = $.parseJSON(formDesignAction)[0];
    console.log(formDesignAction,'formDesignAction');

    action = formDesignAction.action;//add/edit
    edit_data = formDesignAction.edit_data;//编辑的数据
    formListId = formDesignAction.formListId;
    formName = formDesignAction.formName;
}

var editFlag = false;//是否为编辑刚进入页面时
if(action == 'edit') {
    properties = edit_data;
    console.log(properties,'编辑');

    cFormName.setValue(edit_data[0].cFormName);
    var length = edit_data.length;
    var gridId_arr = [];
    var gridTabId_arr = [];
    for(var k=0;k<length;k++) {
        editFlag = true;
        if(edit_data[k].type == 'table') {//表格
            if(gridId_arr.indexOf(edit_data[k].c_id) == -1) {//表格未创建
                gridId_arr.push(edit_data[k].c_id);
                create(edit_data[k].type, [$.parseJSON(edit_data[k].gridColumns),edit_data[k].c_id] );
                setPosition('table', edit_data[k].c_id, edit_data[k].iConX, edit_data[k].iConY, edit_data[k].iWidth, '', edit_data[k].iLabHeight);
            }
        }else if(edit_data[k].type == 'tab') {//页签
            if(tabsId_arr.indexOf(edit_data[k].c_id) == -1) {//tab未创建
                tabsId_arr.push(edit_data[k].c_id);
                create(edit_data[k].type, {data:edit_data[k],i:k});
                setPosition(edit_data[k].type, edit_data[k].c_id, edit_data[k].iConX, edit_data[k].iConY, edit_data[k].iWidth, edit_data[k].iLabWidth, edit_data[k].iLabHeight);
            }else {//tab已创建，插入
                addTab({id: edit_data[k].c_id},{action:'edit',
                    title:edit_data[k].cTabText,name:edit_data[k].tab_id});
            }
        }else if(edit_data[k].type.indexOf('tab_')>-1) {//页签组件
            if(edit_data[k].type == 'tab_table') {//页签表格
                if(gridTabId_arr.indexOf(edit_data[k].c_id) == -1) {//表格未创建
                    gridTabId_arr.push(edit_data[k].c_id);
                    insertTable({id:edit_data[k].tabs_id,tab_id:edit_data[k].tab_id,edit_data:$.parseJSON(edit_data[k].gridColumns),edit_id:edit_data[k].c_id});
                    // create(edit_data[k].type, [$.parseJSON(edit_data[k].gridColumns),edit_data[k].c_id] );
                    setPosition('table', edit_data[k].c_id, edit_data[k].iConX, edit_data[k].iConY, edit_data[k].iWidth, '', edit_data[k].iLabHeight);
                }
            }else {//页签其它组件
                insertBase({id:edit_data[k].tabs_id,tab_id:edit_data[k].tab_id,edit_data:{data:edit_data[k],i:k}},edit_data[k].type.match(/tab_(\S*)/)[1]);
                setPosition(edit_data[k].type, edit_data[k].c_id, edit_data[k].iConX, edit_data[k].iConY, edit_data[k].iWidth, edit_data[k].iLabWidth, edit_data[k].iLabHeight);
            }
        }else {//其它
            create(edit_data[k].type, {data:edit_data[k],i:k});
            setPosition(edit_data[k].type, edit_data[k].c_id, edit_data[k].iConX, edit_data[k].iConY, edit_data[k].iWidth, edit_data[k].iLabWidth, edit_data[k].iLabHeight);
        }
        edit_data[k].cMath = $.parseJSON(edit_data[k].cMath);
    }
    function setPosition(type,id,x,y,w,lab_w,h) {
        $('#'+ id).css({
            clear: 'both',
            left: x,
            top: y,
            width: w,
            height: h,
            position: "absolute"
        })
        if(type == 'table') {

        }else if(type == 'checkbox' && type == 'tab_checkbox') {

        }else {
            $('#lab'+ id).css({
                left: w - lab_w
            })
            $('#left'+ id).css({
                width: w - lab_w,
                // height: h - 2
            })
            $('#right'+ id).css({
                width: lab_w -5 ,
                // height: h - 2,
                left: w - lab_w + 3
            })

        }
        // $("#"+id).offset({top: y + $("#north").height(), left: x + $("#west")});
        // $("#"+thisId).offset({top: offsetY + $("#north").height(), left: offsetX + $("#west").width()});
        // console.log(offsetX,offsetY);
    }
    editFlag = false;
}

//创建组件
function create(type,edit_data) {

    if (type == 'textbox') { // 文本框
        create_textbox('mainField',edit_data);
    } else if (type == 'textarea') { // 文本域
        create_textarea('mainField',edit_data);
    } else if (type == 'datepicker') { //时间
        create_datepicker('mainField',edit_data);
    } else if (type == 'combobox') {//下拉框
        create_combobox('mainField',edit_data);
    } else if (type == 'checkbox') {//复选框
        create_checkbox('mainField',edit_data);
    } else if (type == 'checkboxlist') {//复选框组
        create_checkboxlist('mainField',edit_data);
    } else if (type == 'buttonedit') {//参照
        create_buttonedit('mainField',edit_data);
    } else if (type == 'tab') {//页签
        create_tabs('mainField',edit_data);
    } else if (type == 'table') {//表格
        if(editFlag) {
            create_table('mainField',edit_data[0],edit_data[1]);
        }else {
            create_table('mainField');
        }

    }
}

/**---------------------------------------操作textbox start----------------------------------------*/
//创建文本框
//param: 页签插入组件的参数
// var textbox_index = 0;
function create_textbox(area,edit_data,param) {

    if(action == 'edit' && editFlag) {//编辑模式 非拖拽生成的组件

        var thisId = edit_data.data.c_id;//全称
        var id = thisId.substring(thisId.indexOf("_")+1);//数字
        properties[edit_data.i] = edit_data.data;
        properties[edit_data.i].c_id = thisId;
        textbox_editIndex.push(id);
        textbox_index = Math.max.apply(null, textbox_editIndex);//最大的值作为新建组件的id起点

    }else {
        textbox_index++;
        id = textbox_index;//数字
        thisId = 'textbox_' + id;//全称
        if(area == 'mainField') {
            properties.push({
                c_id: thisId,
                cLadText: '',//字段中文名
                cTableField: '',//字段英文名(唯一)
                cFormName: '',//表单名
                cLength: '',//自定义长度
                cCombo: '',//自定义下拉框内容
                cGridType: '',//自定义列类型
                cMath: '',//公式
                cRefObj: {},
                cTableName: '',//数据库表名(英文)
                tableClassName: '',//数据库表名(中文)
                iConX: '',
                iConY: '',
                iLabHeight: '',
                iWidth: '',
                iLabWidth: '',
                cEditCss: '',
                lanyoutMent: '',
                type: 'textbox'
            });
        }else {//页签插入
            properties.push({
                c_id: thisId,
                tabs_id: param.c_id,
                tab_id: param.tab_id,
                cLadText: '',//字段中文名
                cTableField: '',//字段英文名(唯一)
                cFormName: '',//表单名
                cLength: '',//自定义长度
                cCombo: '',//自定义下拉框内容
                cGridType: '',//自定义列类型
                cMath: '',//公式
                cRefObj: {},
                cTableName: '',//数据库表名(英文)
                tableClassName: '',//数据库表名(中文)
                iConX: '',
                iConY: '',
                iLabHeight: '',
                iWidth: '',
                iLabWidth: '',
                cEditCss: '',
                lanyoutMent: '',
                type: 'tab_textbox'
            });
        }
    }

    var $clone = "<div class='box box-textbox' id='textbox_" + id + "'>" +
        // "  <li style='width: 95px;float: left;height: 100%;background: #ccc;overflow: hidden;text-align: center;line-height: 40px;' id='li" + id + "'></li>" +
        "  <div class='left' id='left" + thisId + "'><p class='text'>textbox</p></div>" +
        "  <label class='lab1' id='lab" + thisId + "'></label>" +
        "  <div class='right' id='right" + thisId + "'>" +
        "       <input id='text"+id+"' class='mini-textbox' emptyText='' />" +
        "  </div>" +
        "</div>";
    var contextmenu = "<ul id='textboxMenu" + id + "' class='mini-contextmenu'>\n" +
        "    <li onclick='removeBox(" + thisId + ")'>删除</li>\n" +
        "</ul>";
    var type;//页签组件或非页签组件类型
    if(area == 'mainField') {
        $('#mainField').append($clone);
        $('#mainField').append(contextmenu);
        type = 'textbox';
    }else {
        $(param.area).append($clone);
        $(param.area).append(contextmenu);
        type = 'tab_textbox';
    }

    mini.parse();
    //拖拽位置
    // $("#"+thisId).offset({top: offsetY + $("#north").height(), left: offsetX + $("#west").width()});
    // console.log(offsetX,offsetY);

    $("#textbox_"+id).bind("contextmenu", function (e) {
        var menu = mini.get("textboxMenu"+id);
        menu.showAtPos(e.pageX, e.pageY);
        return false;
    });

    resize_base("textbox_" + id);

    $("#"+thisId).on('mouseup',function(e){ //点击事件(鼠标松开时发生)
        //click事件里去检测这个变量是否发生改变，如果没改变，说明mouseup刚执行完，这里不执行
        //mouseup事件里定时改变一个变量
        window.mouseup_click_debug = true;
        setTimeout(function() {
            window.mouseup_click_debug = false;
        }, 200);
        baseClick(e,type,thisId);
    });

    setEditBoxText('textbox',thisId);//编辑模式时设置组件名称
}

/**---------------------------------------操作textbox end----------------------------------------*/


/**---------------------------------------操作textarea start-------------------------------------*/
//创建文本域
// var textarea_index = 0;
function create_textarea(area,edit_data,param) {

    if(action == 'edit' && editFlag) {//编辑模式 非拖拽生成的组件

            var thisId = edit_data.data.c_id;
            var id = thisId.substring(thisId.indexOf("_")+1);
            properties[edit_data.i] = edit_data.data;
            properties[edit_data.i].c_id = thisId;
            textarea_editIndex.push(id);
            textarea_index = Math.max.apply(null, textarea_editIndex);

    }else {
        textarea_index++;
        id = textarea_index;//数字
        thisId = 'textarea_'+id;//全称
        if(area == 'mainField') {
            properties.push({
                c_id: thisId,
                cLadText: '',//字段中文名
                cTableField: '',//字段英文名(唯一)
                cFormName: '',//表单名
                cLength: '',//自定义长度
                cCombo: '',//自定义下拉框内容
                cGridType: '',//自定义列类型
                cMath: '',//公式
                cRefObj: {},
                cTableName: '',//数据库表名(英文)
                tableClassName: '',//数据库表名(中文)
                iConX: '',
                iConY: '',
                iLabHeight: '',
                iWidth: '',
                iLabWidth: '',
                cEditCss: '',
                lanyoutMent: '',
                type: 'textarea'
            });
        }else {//页签插入
            properties.push({
                c_id: thisId,
                tabs_id: param.c_id,
                tab_id: param.tab_id,
                cLadText: '',//字段中文名
                cTableField: '',//字段英文名(唯一)
                cFormName: '',//表单名
                cLength: '',//自定义长度
                cCombo: '',//自定义下拉框内容
                cGridType: '',//自定义列类型
                cMath: '',//公式
                cRefObj: {},
                cTableName: '',//数据库表名(英文)
                tableClassName: '',//数据库表名(中文)
                iConX: '',
                iConY: '',
                iLabHeight: '',
                iWidth: '',
                iLabWidth: '',
                cEditCss: '',
                lanyoutMent: '',
                type: 'tab_textarea'
            });
        }
    }

    var $clone = "<div class='box box-textarea' id='textarea_" + id + "'>" +
        "  <div class='left' id='left" + thisId + "'><p class='text'>texterea</p></div>" +
        "  <label class='lab1' id='lab" + thisId + "'></label>" +
        "  <div class='right' id='right" + thisId + "'>" +
        "     <textarea class='mini-textarea' emptyText=''></textarea>" +
        "  </div>" +
        "</div>";
    var contextmenu = "<ul id='textareaMenu" + id + "' class='mini-contextmenu'>\n" +
        "    <li onclick='removeBox(" + thisId + ")'>删除</li>\n" +
        "</ul>";
    var type;
    if(area == 'mainField') {
        $('#mainField').append($clone);
        $('#mainField').append(contextmenu);
        type = 'textarea';
    }else {
        $(param.area).append($clone);
        $(param.area).append(contextmenu);
        type = 'tab_textarea';
    }
    mini.parse();

    $("#textarea_"+id).bind("contextmenu", function (e) {
        var menu = mini.get("textareaMenu"+id);
        menu.showAtPos(e.pageX, e.pageY);
        return false;
    });
    resize_base("textarea_" + id);
    $("#"+thisId).on('mouseup',function(e){
        baseClick(e,type,thisId)
    });

    setEditBoxText('textarea',thisId);//编辑模式时设置组件名称

}

/**---------------------------------------操作textarea end-----------------------------*/


/**---------------------------------------操作datepicker start-------------------------------------*/
//创建时间选择器
// var datepicker_index = 0;
function create_datepicker(area,edit_data,param) {

    if(action == 'edit' && editFlag) {//编辑模式 非拖拽生成的组件

            var thisId = edit_data.data.c_id;
            var id = thisId.substring(thisId.indexOf("_")+1);
            properties[edit_data.i] = edit_data.data;
            properties[edit_data.i].c_id = thisId;
            datepicker_editIndex.push(id);
            datepicker_index = Math.max.apply(null, datepicker_editIndex);

    }else {
        datepicker_index++;
        id = datepicker_index;//数字
        thisId = 'datepicker_'+id;//全称
        if(area == 'mainField') {
            properties.push({
                c_id: thisId,
                cLadText: '',//字段中文名
                cTableField: '',//字段英文名(唯一)
                cFormName: '',//表单名
                cLength: '',//自定义长度
                cCombo: '',//自定义下拉框内容
                cGridType: '',//自定义列类型
                cMath: '',//公式
                cRefObj: {},
                cTableName: '',//数据库表名(英文)
                tableClassName: '',//数据库表名(中文)
                iConX: '',
                iConY: '',
                iLabHeight: '',
                iWidth: '',
                iLabWidth: '',
                cEditCss: '',
                lanyoutMent: '',
                type: 'datepicker'
            });
        }else {//页签插入
            properties.push({
                c_id: thisId,
                tabs_id: param.c_id,
                tab_id: param.tab_id,
                cLadText: '',//字段中文名
                cTableField: '',//字段英文名(唯一)
                cFormName: '',//表单名
                cLength: '',//自定义长度
                cCombo: '',//自定义下拉框内容
                cGridType: '',//自定义列类型
                cMath: '',//公式
                cRefObj: {},
                cTableName: '',//数据库表名(英文)
                tableClassName: '',//数据库表名(中文)
                iConX: '',
                iConY: '',
                iLabHeight: '',
                iWidth: '',
                iLabWidth: '',
                cEditCss: '',
                lanyoutMent: '',
                type: 'tab_datepicker'
            });
        }
    }

    var $clone = "<div class='box box-datepicker' id='datepicker_" + id + "'>" +
        "  <div class='left' id='left" + thisId + "'><p class='text'>datepicker</p></div>" +
        "  <label class='lab1' id='lab" + thisId + "'></label>" +
        "  <div class='right' id='right" + thisId + "'>" +
        "    <input id='date" + id + "'class='mini-datepicker' value=''/>" +
        "  </div>" +
        "</div>";
    var contextmenu = "<ul id='datepickerMenu" + id + "' class='mini-contextmenu'>\n" +
        "    <li onclick='removeBox(" + thisId + ")'>删除</li>\n" +
        "</ul>";

    var type;
    if(area == 'mainField') {
        $('#mainField').append($clone);
        $('#mainField').append(contextmenu);
        type = 'datepicker';
    }else {
        $(param.area).append($clone);
        $(param.area).append(contextmenu);
        type = 'tab_datepicker';
    }
    mini.parse();
    setEditBoxText('datepicker',thisId);//编辑模式时设置组件名称

    $("#datepicker_"+id).bind("contextmenu", function (e) {
        var menu = mini.get("datepickerMenu"+id);
        menu.showAtPos(e.pageX, e.pageY);
        return false;
    });
    // disableDate("date" + id);
    resize_base(thisId);
    $("#"+thisId).on('mouseup',function(e){
        baseClick(e,type,thisId)
    });
    mini.get("date"+id).on('valuechanged',function(e){//组件值恒为空
        e.sender.setValue("");
    });

    setEditBoxText('textbox',thisId);//编辑模式时设置组件名称

}

//禁用
// function disableDate(id) {
//     var date = mini.get(id+'');
//     date.disable();
// }

/**---------------------------------------操作datepicker end---------------------*/


/**---------------------------------------参照 start-------------------------------*/
//创建
// var buttonedit_index = 0;
function create_buttonedit(area,edit_data,param) {
    if(action == 'edit' && editFlag) {//编辑模式 非拖拽生成的组件

            var thisId = edit_data.data.c_id;
            var id = thisId.substring(thisId.indexOf("_") + 1);
            properties[edit_data.i] = edit_data.data;
            properties[edit_data.i].c_id = thisId;
            buttonedit_editIndex.push(id);
            buttonedit_index = Math.max.apply(null, buttonedit_editIndex);

    }else {
        buttonedit_index++;
        id = buttonedit_index;//数字
        thisId = 'buttonedit_'+id;//全称
        if(area == 'mainField') {
            properties.push({
                c_id: thisId,
                cLadText: '',//字段中文名
                cTableField: '',//字段英文名(唯一)
                cFormName: '',//表单名
                cLength: '',//自定义长度
                cReference: '',//参照
                cBgReference: '',//后台参照
                cCombo: '',//自定义下拉框内容
                cGridType: '',//自定义列类型
                cMath: '',//公式
                cRefObj: {},
                cTableName: '',//数据库表名(英文)
                tableClassName: '',//数据库表名(中文)
                iConX: '',
                iConY: '',
                iLabHeight: '',
                iWidth: '',
                iLabWidth: '',
                cEditCss: '',
                lanyoutMent: '',
                type: 'buttonedit'
            });
        }else {//页签插入
            properties.push({
                c_id: thisId,
                tabs_id: param.c_id,
                tab_id: param.tab_id,
                cLadText: '',//字段中文名
                cTableField: '',//字段英文名(唯一)
                cFormName: '',//表单名
                cLength: '',//自定义长度
                cReference: '',//参照
                cBgReference: '',//后台参照
                cCombo: '',//自定义下拉框内容
                cGridType: '',//自定义列类型
                cMath: '',//公式
                cRefObj: {},
                cTableName: '',//数据库表名(英文)
                tableClassName: '',//数据库表名(中文)
                iConX: '',
                iConY: '',
                iLabHeight: '',
                iWidth: '',
                iLabWidth: '',
                cEditCss: '',
                lanyoutMent: '',
                type: 'tab_buttonedit'
            });
        }
    }
    var $clone = "<div class='box box-buttonedit' id='buttonedit_" + id + "'>" +
        "  <div class='left' id='left" + thisId + "'><p class='text'>buttonedit</p></div>" +
        "  <label class='lab1' id='lab" + thisId + "'></label>" +
        "  <div class='right' id='right" + thisId + "'>" +
        "   <input id='look"+id+"' class='mini-buttonedit' emptyText='' />" +
        "</div></div>";
    var contextmenu = "<ul id='buttoneditMenu" + id + "' class='mini-contextmenu'>\n" +
        "    <li onclick='removeBox(" + thisId + ")'>删除</li>\n" +
        "</ul>";

    var type;
    if(area == 'mainField') {
        $('#mainField').append($clone);
        $('#mainField').append(contextmenu);
        type = 'buttonedit';
    }else{
        $(param.area).append($clone);
        $(param.area).append(contextmenu);
        type = 'tab_buttonedit';
    }

    mini.parse();

    $("#buttonedit_"+id).bind("contextmenu", function (e) {
        var menu = mini.get("buttoneditMenu"+id);
        menu.showAtPos(e.pageX, e.pageY);
        return false;
    });
    $("#" + thisId).draggable({
        containment: "parent",
        // containment: "#mainField",
        scroll: false,
        cursor: "move"
    });
    resize_base(thisId);
    $("#"+thisId).on('mouseup',function(e){
        baseClick(e,type,thisId)
    });

    setEditBoxText('buttonedit',thisId);//编辑模式时设置组件名称

}

//参照弹窗
cReference.on("focus", function (e) {
    onBindRef();
})
cBgReference.on("focus", function (e) {
    onBindRef();
})

function onBindRef() {
    mini.open({
        url : bootPATH + "../frame/frame1/pages/formDesign/bindRef.html",
        title : "选择参照",
        onload: function () {       //弹出页面加载完成
            // var iframe = this.getIFrameEl();
            // var data = { action: "edit-db", id: id, properties: properties };
            // iframe.contentWindow.SetDbData(data);
        },
        ondestroy: function (action) {  //弹出页面关闭前
            if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.onTableOk();
                data = mini.clone(data);
                console.log(data,'bindKey');

                cReference.setValue();
                cBgReference.setValue();
                // cLadText.doValueChanged();//主动激发valuechanged事件

            }
        }
    })
}
/**---------------------------------------参照 end--------------------------------------------*/


/**---------------------------------------下拉框 start----------------------------------------*/
// var combobox_index = 0;
function create_combobox(area,edit_data,param) {
    if(action == 'edit' && editFlag) {//编辑模式 非拖拽生成的组件

            var thisId = edit_data.data.c_id;
            var id = thisId.substring(thisId.indexOf("_")+1);
            properties[edit_data.i] = edit_data.data;
            properties[edit_data.i].c_id = thisId;
            combobox_editIndex.push(id);
            combobox_index = Math.max.apply(null, combobox_editIndex);

    }else {
        combobox_index++;
        id = combobox_index;//数字
        thisId = 'combobox_'+id;//全称
        if(area == 'mainField') {
            properties.push({
                c_id: thisId,
                cLadText: '',//字段中文名
                cTableField: '',//字段英文名(唯一)
                cFormName: '',//表单名
                cLength: '',//自定义长度
                cCombo: '',//自定义下拉框内容
                cGridType: '',//自定义列类型
                cMath: '',//公式
                cRefObj: {},
                cTableName: '',//数据库表名(英文)
                tableClassName: '',//数据库表名(中文)
                iConX: '',
                iConY: '',
                iLabHeight: '',
                iWidth: '',
                iLabWidth: '',
                cEditCss: '',
                lanyoutMent: '',
                type: 'combobox'
            });
        }else {//页签插入
            properties.push({
                c_id: thisId,
                tabs_id: param.c_id,
                tab_id: param.tab_id,
                cLadText: '',//字段中文名
                cTableField: '',//字段英文名(唯一)
                cFormName: '',//表单名
                cLength: '',//自定义长度
                cCombo: '',//自定义下拉框内容
                cGridType: '',//自定义列类型
                cMath: '',//公式
                cRefObj: {},
                cTableName: '',//数据库表名(英文)
                tableClassName: '',//数据库表名(中文)
                iConX: '',
                iConY: '',
                iLabHeight: '',
                iWidth: '',
                iLabWidth: '',
                cEditCss: '',
                lanyoutMent: '',
                type: 'tab_combobox'
            });
        }
    }

    var $clone = "<div class='box box-combobox' id='combobox_" + id + "'>" +
        // "  <li style='width: 95px;float: left;height: 100%;background: #ccc;overflow: hidden;text-align: center;line-height: 40px;' id='li" + id + "'></li>" +
        "  <div class='left' id='left" + thisId + "'><p class='text'>combobox</p></div>" +
        "  <label class='lab1' id='lab" + thisId + "'></label>" +
        "  <div class='right' id='right" + thisId + "'>" +
        "    <input id='combo"+id+"' class='mini-combobox' style=''" +
        "     textField='text' valueField='id' value=''\n" +
        "     showNullItem='true' allowInput='false'/>";
        " </div>" +
        "</div>";

    var type;
    var contextmenu = "<ul id='comboboxMenu" + id + "' class='mini-contextmenu'>\n" +
        "    <li onclick='removeBox(" + thisId + ")'>删除</li>\n" +
        "</ul>";
    if(area == 'mainField') {
        $('#mainField').append($clone);
        $('#mainField').append(contextmenu);
        type = 'combobox';
    }else {
        $(param.area).append($clone);
        $(param.area).append(contextmenu);
        type = 'tab_combobox';
    }
    mini.parse();

    $("#combobox_"+id).bind("contextmenu", function (e) {
        var menu = mini.get("comboboxMenu"+id);
        menu.showAtPos(e.pageX, e.pageY);
        return false;
    });

    resize_base("combobox_" + id);
    $("#"+thisId).on('mouseup',function(e){
        baseClick(e,type,thisId)
    });

    setEditBoxText('combobox',thisId);//编辑模式时设置组件名称
}

/**---------------------------------------复选框 end----------------------------------------*/

/**---------------------------------------复选框组 start----------------------------------------*/
//创建
// var checkboxlist_index = 0;
function create_checkboxlist(area,edit_data,param) {
    if(action == 'edit' && editFlag) {//编辑模式 非拖拽生成的组件

    }else {

    }
}

/**---------------------------------------下拉框组  end ----------------------------------------*/

/**---------------------------------------复选框 start----------------------------------------*/
//创建
// var checkbox_index = 0;8
function create_checkbox(area,edit_data,param) {
    if(action == 'edit' && editFlag) {//编辑模式 非拖拽生成的组件

        var thisId = edit_data.data.c_id;
        var id = thisId.substring(thisId.indexOf("_")+1);
        properties[edit_data.i] = edit_data.data;
        properties[edit_data.i].c_id = thisId;
        checkbox_editIndex.push(id);
        checkbox_index = Math.max.apply(null, checkbox_editIndex);

    }else {
        checkbox_index++;
        id = checkbox_index;//数字
        thisId = 'checkbox_'+id;//全称
        if(area == 'mainField') {
            properties.push({
                c_id: thisId,
                cLadText: '',//字段中文名
                cTableField: '',//字段英文名(唯一)
                cFormName: '',//表单名
                cLength: '',//自定义长度
                cGridType: '',//自定义列类型
                cMath: '',//公式
                cRefObj: {},
                cTableName: '',//数据库表名(英文)
                tableClassName: '',//数据库表名(中文)
                iConX: '',
                iConY: '',
                iLabHeight: '',
                iWidth: '',
                iLabWidth: '',
                cEditCss: '',
                lanyoutMent: '',
                type: 'checkbox'
            });
        }else {//页签插入
            properties.push({
                c_id: thisId,
                tabs_id: param.c_id,
                tab_id: param.tab_id,
                cLadText: '',//字段中文名
                cTableField: '',//字段英文名(唯一)
                cFormName: '',//表单名
                cLength: '',//自定义长度
                cGridType: '',//自定义列类型
                cMath: '',//公式
                cRefObj: {},
                cTableName: '',//数据库表名(英文)
                tableClassName: '',//数据库表名(中文)
                iConX: '',
                iConY: '',
                iLabHeight: '',
                iWidth: '',
                iLabWidth: '',
                cEditCss: '',
                lanyoutMent: '',
                type: 'tab_checkbox'
            });
        }
    }

    var $clone = "<div class='box-checkbox' id='checkbox_"+id+"'>" +
        "<div id='check_"+id+"' name='' class='mini-checkbox' readOnly='false' text='checkBox'></div>" +
        "</div>";

    var contextmenu = "<ul id='checkboxMenu" + id + "' class='mini-contextmenu'>\n" +
        "    <li onclick='removeBox(" + thisId + ")'>删除</li>\n" +
        "</ul>";

    var type;
    if(area == 'mainField') {
        $('#mainField').append($clone);
        $('#mainField').append(contextmenu);
        type = 'checkbox';
    }else {
        $(param).append($clone);
        $(param).append(contextmenu);
        type = 'tab_checkbox';
    }

    mini.parse();

    $("#checkbox_"+id).bind("contextmenu", function (e) {
        var menu = mini.get("checkboxMenu"+id);
        menu.showAtPos(e.pageX, e.pageY);
        return false;
    });
    $("#" + thisId).draggable({
        containment: "parent",
        // containment: "#mainField",
        scroll: false,
        cursor: "move"
    });
    $("#" + thisId).resizable({
        handles: 'n, e, s, w, ne, se, sw, nw',
        grid: [2, 2],
        resize: function () {
            $("#" + thisId).css({
                "min-height": $("#" + thisId+' span.mini-checkbox').height(),
                "min-width": $("#" + thisId+' span.mini-checkbox').width() +20
            })
        }
    });

    $("#"+thisId).on('mouseup',function(e){
        baseClick(e,type,thisId);
    });

    setEditBoxText('checkbox',thisId);//编辑模式时设置组件名称

}

/**---------------------------------------复选框 end-------------------------------------*/


/**---------------------------------------操作表格 start----------------------------------------*/
//创建表格
// var table_index = 0;
function create_table(area,edit_data,edit_id,param) {
    // table_index++;
    // var id = table_index;
    // var thisId = "datagrid_" + id;
    var cols;
    if(action == 'edit' && editFlag) {
        cols = edit_data;
        for(var i=0;i<cols.length;i++) {
            cols[i].editor = {type: edit_data[i].editor.type};
        }
        var thisId = edit_id;
        var id = thisId.substring(thisId.indexOf("_")+1);
        table_editIndex.push(id);
        table_index = Math.max.apply(null, table_editIndex);
    }else {
        table_index++;
        id = table_index;//数字
        thisId = 'datagrid_'+id;//全称
        cols = [{
            field: thisId+'_field1', header: '列1',name: thisId+'_field1', width: 100, headerAlign: "center", allowSort: false,
            editor: { type: "textbox"}
        }, {
            field: thisId+'_field2', header: '列2',name: thisId+'_field2', width: 100, headerAlign: "center", allowSort: false,
            editor: { type: "textbox"}
        }];
        for(var i=0;i<cols.length;i++) {
            if(area == 'mainField') {
                properties.push({
                    c_id: thisId,//表格id
                    col_id: cols[i].field,//列id
                    cLadText: cols[i].header,//字段中文名
                    cTableField: cols[i].field,//字段英文名(唯一)
                    cFormName: '',//表单名
                    cLength: '',//自定义长度
                    cCombo: '',//自定义下拉框内容
                    cGridType: 'textbox',//自定义列类型
                    cReference: '',
                    cBgReference: '',
                    cMath: '',//公式
                    cRefObj: {},
                    cTableName: '',//数据库表名(英文)
                    tableClassName: '',//数据库表名(中文)
                    iConX: '',
                    iConY: '',
                    iLabHeight: '',
                    iWidth: '',
                    iLabWidth: '',
                    cEditCss: '',
                    lanyoutMent: '',
                    type: 'table'
                });
            }else {//页签插入
                properties.push({
                    c_id: thisId,//表格id
                    col_id: cols[i].field,//列id
                    tabs_id: param.c_id,
                    tab_id: param.tab_id,
                    cLadText: cols[i].header,//字段中文名
                    cTableField: cols[i].field,//字段英文名(唯一)
                    cFormName: '',//表单名
                    cLength: '',//自定义长度
                    cCombo: '',//自定义下拉框内容
                    cGridType: 'textbox',//自定义列类型
                    cReference: '',
                    cBgReference: '',
                    cMath: '',//公式
                    cRefObj: {},
                    cTableName: '',//数据库表名(英文)
                    tableClassName: '',//数据库表名(中文)
                    iConX: '',
                    iConY: '',
                    iLabHeight: '',
                    iWidth: '',
                    iLabWidth: '',
                    cEditCss: '',
                    lanyoutMent: '',
                    type: 'tab_table'
                });
            }
        }
    }

    var $clone = "<div id='datagrid_" + id + "' class='mini-datagrid' style=''\n" +
        "     showPager=false multiSelect=false allowResize=true allowColumnSelect=true" +
        "     allowCellEdit=true allowCellSelect=true editNextOnEnterKey=true editNextRowCell=true "+
        "     headerContextMenu='#headerMenu" + id + "' contextMenu='#gridMenu" + id + "'>\n" +
        "    <div property='columns'></div>\n" +
        "</div>";
    var headerMenu = "<ul id='headerMenu" + id + "' class='mini-contextmenu'>\n" +
        "    <li name='add' onclick='addGridCols(" + id + ")'>增加列</li>\n" +
        "    <li name='remove' onclick='removeGridCols(" + id + ")'>删除列</li>\n" +
        "    <li name='delAll' onclick='removeAllGridCols(" + id + ")'>全部删除</li>\n" +
        // "    <li><span >列类型</span><ul>" +
        // "    <li name='set' onclick=\"setGridColEditor(" + id + ",'textbox')\">文本</li>\n" +
        // "    <li name='set' onclick=\"setGridColEditor(" + id + ",'textarea')\">文本域</li>\n" +
        // "    <li name='set' onclick=\"setGridColEditor(" + id + ",'combobox')\">下拉框</li>\n" +
        // "    <li name='set' onclick=\"setGridColEditor(" + id + ",'datepicker')\">时间</ul></li>\n" +
        "</ul>";

    var gridMenu = "<ul id='gridMenu" + id + "' class='mini-contextmenu' onbeforeopen='onBeforeOpenGrid(e," + id + ")'>\n" +
        "    <li onclick='addGridRows(" + id + ")'>新增行</li>\n" +
        "    <li onclick='removeGridRows(" + id + ")'>删除行</li>\n" +
        // "    <li onclick='removeAllTab("+id+")'>全部删除</li>\n" +
        "</ul>";

    var type;
    if(area == 'tabs') {
        $(param.area).append($clone);
        $(param.area).append(headerMenu);
        $(param.area).append(gridMenu);
        type = 'tab_table';
    }else{
        $('#mainField').append($clone);
        $('#mainField').append(headerMenu);
        // $('#mainField').append(gridMenu);
        type = 'table';
    }

    mini.parse();

    setEditBoxText('table',thisId);//编辑模式时设置组件名称
    var grid = mini.get("datagrid_" + id);
    grid.set({
        columns: cols
    })
    var data = [{}, {}];
    grid.setData(data);

    $("#" + thisId).draggable({
        containment: "parent",
        // containment: "#mainField",
        scroll: false,
        cursor: "move"
    });

    //点击表头事件
    grid.on("headercellclick", function (e) {
        gridClick(e,type);
    });

    //右击表头事件
    grid.on("headercellcontextmenu", function (e) {
        // console.log(e);
        rightClickCol = e.column;
    });

}

//表格--点击表头
var c_id;//总id
function gridClick(e,type) {

    // $(".mini-grid-headerCell").css({
    //     background: "red"
    // });
    // e.htmlEvent.target.parentNode.style.background = '#b3d5ea';
    stopBubbling(e,'table');//多个点击事件冲突处理
    cancelBlur();
    pre_click_type = click_type;
    pre_click_id = click_id;
    click_type = type;
    click_types.push(type);
    // click_id = e.column.field;//field是id英文,header是中文
    click_id = e.column.name;//name字段为固定
    click_ids.push(click_id);
    c_id = e.sender.id;//表格id
    console.log('pre:',pre_click_type,',',pre_click_id);
    console.log('now:',click_type,',',click_id);

    showProperty("table");

    savePre(pre_click_type,click_id,pre_click_id);

    // console.log(e.sender,'sender');//表格对象
    // var cols = e.sender.columns;//所有的列
    // var grid = mini.get(e.sender.id);
    // console.log(e.column, 'cols');//列对象
    //
    // for(var i=0;i<cols.length;i++) {
    //     if(cols[i].field == e.column.field) {
    //         cols[i].field = 'test';
    //         cols[i].header = '测试';
    //     }
    // }
    // grid.set({
    //     columns: cols
    // })
}
//表格--右键菜单
function onBeforeOpenGrid(e,id) {
    var grid = mini.get("datagrid"+id);
    var menu = e.sender;

    var row = grid.getSelected();
    // var rowIndex = grid.indexOf(row);
    if (!row) {
        e.cancel = true;
        e.htmlEvent.preventDefault();//阻止浏览器默认右键菜单
        return;
    }
    // var editItem = mini.getbyName("edit", menu);
    // editItem.show();
    // removeItem.enable();
}
//增列
var col_index = 2;
var _id_arr = [];
function addGridCols(id) {
    var grid = mini.get("datagrid_" + id);
    var arr = grid.getColumns();
    col_index++;

    for(var j=0;j<arr.length;j++) {
        _id_arr.push(arr[j]._id);
    }
    var _id = Math.max.apply(null, _id_arr)+1;//新增的列id取最大值+1,防止_id冲突
    arr.push({
        field: "datagrid_" + id+'_field'+col_index,
        header: '列'+col_index,
        name: "datagrid_" + id+'_field'+col_index,
        width:100, headerAlign: "center", allowSort: false,
        editor: {type:'textbox'},
        _id: _id
    });

    grid.setColumns(arr);

    console.log(grid.getColumns());
    var type;
    var tabs_id;
    var tab_id;
    for(var i=0;i<properties.length;i++) {
        if(properties[i].c_id == "datagrid_" + id) {
            type = properties[i].type;
            if(type == 'table') {
                tabs_id = '';
                tab_id = '';
            }else {
                tabs_id = properties[i].tabs_id;
                tab_id = properties[i].tab_id;
            }
        }
    }
    properties.push({
        c_id: "datagrid_" + id,//表格id
        col_id:  "datagrid_" + id+'_field'+col_index,//列id
        cLadText: '列'+col_index,//字段中文名
        cTableField: "datagrid_" + id+'_field'+col_index,//字段英文名(唯一)
        cFormName: '',//表单名
        cLength: '',//自定义长度
        cCombo: '',//自定义下拉框内容
        cGridType: 'textbox',//自定义列类型
        cMath: '',//公式
        tab_id: tab_id,
        tabs_id: tabs_id,
        cRefObj: {},
        cTableName: '',//数据库表名(英文)
        tableClassName: '',//数据库表名(中文)
        iConX: '',
        iConY: '',
        iLabHeight: '',
        iWidth: '',
        iLabWidth: '',
        cEditCss: '',
        lanyoutMent: '',
        type: type
    });
    // grid.on("columnschanged",function (e) {
    //     console.log(e);
    // })

}
//删除指定列
function removeGridCols(id) {
    var grid = mini.get("datagrid_" + id);
    var arr = grid.getColumns();
    if(arr.length > 1) {
        // var index = arr.indexOf(rightClickCol);
        // arr.splice(index, 1)[0];//删除列数据
        for(var j=0;j<arr.length;j++) {
            if(arr[j].name == rightClickCol.name) {
                arr.splice(j,1)[0];//删除列数据
            }
        }
        console.log(arr);
        grid.set({
            columns: arr
        });

        for(var i=0;i<properties.length;i++) {
            if(properties[i].col_id == rightClickCol.name) {
                properties.splice(i, 1);
            }
        }
        console.log(properties);
    }else {
        removeAllGridCols(id);
    }
}
//删除整表
function removeAllGridCols(id) {
    var grid = mini.get("datagrid_" + id);
    $("#datagrid_" + id).remove();
    grid.destroy();

    properties = properties.filter(function(x){
        if("datagrid_" + id == x.c_id){
            return false
        }
        else{return true}
    });

    console.log(properties);
}
//右键菜单设置列类型
function setGridColEditor(id,type) {
    console.log(id,type);
    console.log(rightClickCol);
    var grid = mini.get("datagrid_" + id);
    var cols = grid.getColumns();

    for(var i=0;i<cols.length;i++) {
        if(cols[i].field == rightClickCol.field) {
            grid.updateColumn(rightClickCol.name, {editor: { type: type}});
        }
    }
    // console.log(grid.getColumns());

    // for(var i=0;i<cols.length;i++) {
    //     if(cols[i].field == rightClickCol.field) {
    //         cols[i].editor = { type: type};
    //     }
    // }
    // grid.set({
    //     columns: cols
    // });
}
/**---------------------------------------操作表格 end----------------------------------------*/


/**---------------------------------------操作tabs start-----------------------------------------*/
//创建页签
// var tabs_index = 0;
function create_tabs(area,edit_data) {
    // tabs_index++;
    // var id = tabs_index;
    // var thisId = 'tabs_' + id;
    var name = 'tab1';
    var title = 'tab1';

    if(action == 'edit' && editFlag) {
        var thisId = edit_data.data.c_id;//全称
        var id = thisId.substring(thisId.indexOf("_")+1);//数字
        properties[edit_data.i] = edit_data.data;
        properties[edit_data.i].c_id = thisId;
        name = edit_data.data.tab_id;
        title = edit_data.data.cTabText;
        tab_editIndex.push(id);
        tab_index = Math.max.apply(null, tab_editIndex);//最大的值作为新建组件的id起点

    }else {
        tab_index++;
        id = tab_index;//数字
        thisId = 'tab_'+id;//全称
        name = thisId+name;
        properties.push({
            c_id: thisId,//tab id
            tab_id: name,//tab单页id
            cLadText: '',//字段中文名
            cTabText: 'tab1',//字段中文名
            cTableField: '',//字段英文名(唯一)
            cFormName: '',//表单名
            cLength: '',//自定义长度
            cCombo: '',//自定义下拉框内容
            cGridType: '',//自定义列类型
            cReference: '',
            cBgReference: '',
            cMath: '',//公式
            cRefObj: {},
            cTableName: '',//数据库表名(英文)
            tableClassName: '',//数据库表名(中文)
            iConX: '',
            iConY: '',
            iLabHeight: '',
            iWidth: '',
            iLabWidth: '',
            cEditCss: '',
            lanyoutMent: '',
            type: 'tab'
        });
    }

    var $clone = "<div id='" + thisId + "' class='mini-tabs' activeIndex='0' style='width:350px;height:200px;'\n" +
        "    contextMenu='#tabsMenu" + id + "'>\n" +
        "        <div name='"+name+"' title='"+title+"'> </div>\n" +
        "     </div>";

    var contextMenu = "<ul id='tabsMenu" + id + "' class='mini-contextmenu' onbeforeopen='onBeforeOpenTab(e)'>\n" +
        "    <li onclick='addTab(" + thisId + ")'>新增</li>\n" +
        "    <li onclick='removeTab(" + thisId + ")'>删除</li>\n" +
        "    <li onclick='removeAllTab(" + thisId + ")'>全部删除</li>\n" +
        "    <li><span >插入</span><ul>" +
        "       <li onclick='insertTable(" + thisId + ")'>表格</li>\n" +
        "       <li onclick=\"insertBase(" + thisId + ",'textbox')\">文本框</li>\n" +
        "       <li onclick=\"insertBase(" + thisId + ",'textarea')\">文本域</li>\n" +
        "       <li onclick=\"insertBase(" + thisId + ",'datepicker')\">时间选择器</li>\n" +
        "       <li onclick=\"insertBase(" + thisId + ",'buttonedit')\">参照</li>\n" +
        "       <li onclick=\"insertBase(" + thisId + ",'combobox')\">下拉框</li>\n" +
        "       <li onclick=\"insertBase(" + thisId + ",'checkbox')\">复选框</li>\n" +
        "       <li onclick=\"insertBase(" + thisId + ",'checkboxlist')\">复选框组</li>" +
        "    </ul></li>\n" +
        "</ul>";
    if(area == 'mainField') {
        $('#mainField').append($clone);
        $('#mainField').append(contextMenu);
    }
    mini.parse();

    $("#" + thisId).resizable({
        resize: function () {
            // console.log($("#" + id).height(),'height');

            $("#" + thisId + " .mini-tabs-bodys").css({
                height: $("#" + thisId).height() - 33 - 11,
                width: $("#" + thisId).width() - 12
            });
            $("#" + thisId + " .mini-tabs-scrollCt").css({
                width: $("#" + thisId).width()
            });
            $("#" + thisId + " .mini-tabs-body").css({
                height: '100%'
            });
        }
    });
    $("#" + thisId).draggable({
        // containment: "parent",
        containment: "#mainField",
        scroll: false,
        cursor: "move"
    });
    // $("#" + thisId).click(tabClick);
    // $("#" + thisId).dblclick(tabClick(thisId));
    mini.get(thisId).on("activeChanged",function (e) {
        tabClick(thisId);
    })

    $("#" + thisId).dblclick(function () {
        tabClick(thisId);
    })
    // $("#" + thisId).click(function () {
    //     tabs_id = $(this).attr('id');
    //     console.log(tabs_id,'当前tabs');
    //     var tabs = mini.get(tabs_id);
    //     tabs.activeTab(tabs.getActiveTab());
    // })

}

//tab--点击事件
function tabClick(id) {
    // stopBubbling();
    console.log(properties,'properties');
    // var id = $(this).attr('id');
    var tabs = mini.get(id);

    setTimeout(function () {
        var tab = tabs.getActiveTab();
        console.log(tab,'active tab');

        cancelBlur();
        pre_click_type = click_type;//为空则为第一次点击，不存储
        pre_click_id = click_id;
        // var pre_click_id = click_ids.slice(-2, -1).toString();
        click_type = 'tab';
        click_types.push('tab');
        click_id = tab.name;
        c_id = id;
        click_ids.push(click_id);
        showProperty('tab');

        console.log('--------------------------------');
        console.log('pre:',pre_click_type,',',pre_click_id);
        console.log('now:',click_type,',',click_id);
        savePre(pre_click_type,click_id,pre_click_id);

    },100);
}

//tab右键点击菜单弹出之前
var currentTab = null;//右键操作的菜单
function onBeforeOpenTab(e) {
    var id = e.popupEl.id;
    var tabs = mini.get(id);
    currentTab = tabs.getTabByEvent(e.htmlEvent);
    // tabs.activeTab(currentTab);
    if (!currentTab) {
        e.cancel = true;
    }
}
function addTab(e,param) {

    var tabs = mini.get(e.id);
    if(action == 'edit' && editFlag) {
        var name = param.name;
        var tab_id = name;
        var tab = {title: param.title, name: name};
    }else {
        var name = "tab"+tab_index++;
        var tab_id = e.id+name;
        var tab = {title: name, name: e.id+name};
        properties.push({
            c_id: e.id,//tab id
            tab_id: tab_id,//tab单页id
            cTabText: name,
            cLadText: name,//字段中文名
            cTableField: '',//字段英文名(唯一)
            cFormName: '',//表单名
            cLength: '',//自定义长度
            cCombo: '',//自定义下拉框内容
            cGridType: '',//自定义列类型
            cReference: '',
            cBgReference: '',
            cMath: '',//公式
            cRefObj: {},
            cTableName: '',//数据库表名(英文)
            tableClassName: '',//数据库表名(中文)
            iConX: '',
            iConY: '',
            iLabHeight: '',
            iWidth: '',
            iLabWidth: '',
            cEditCss: '',
            lanyoutMent: '',
            type: 'tab'
        });
    }
    tab = tabs.addTab(tab);
    tabs.activeTab(tab);


    // var el = tabs.getTabBodyEl(tab);
    // el.innerHTML = index;
}
function removeTab(e) {//右键菜单移除
    var tabs = mini.get(e.id);
    // console.log(tabs.getTabs());
    if(tabs.getTabs().length > 1) {
        tabs.removeTab(currentTab);
        properties = properties.filter(function(x){//删除数据
            if(currentTab.name == x.tab_id){
                return false
            }
            else{return true}
        })
        console.log(properties,'删除');
    }else {//最后一个tab
        removeAllTab(e);//删除整个页签
    }
}
function removeAllTab(e) {
    // var tabs = mini.get(id+'');
    // tabs.removeAll();
    $('#'+ e.id).remove();//删除整个页签
    properties = properties.filter(function(x){//删除数据
        if(e.id == x.c_id){
            return false
        }
        else{return true}
    })
}
//插入表格
function insertTable(e) {
    var tabs = mini.get(e.id);
    // console.log(currentTab);
    var tabEl = tabs.getTabBodyEl(currentTab);
    var edit_data = [];
    var edit_id = '';

    if(action == 'edit' && editFlag) {
        currentTab = tabs.getTab(e.tab_id);
        tabEl = tabs.getTabBodyEl(currentTab);
        edit_data = e.edit_data;
        edit_id = e.edit_id;
    }
    var param = {
        area: "div[id='" + tabEl.id + "']",
        c_id: e.id,//整个页签
        tab_id: currentTab.name//插入的当前页签
    }
    create_table('tabs',edit_data,edit_id,param);
}
//插入其他组件
function insertBase(e,type) {
    var tabs = mini.get(e.id);
    // console.log(currentTab);
    var tabEl = tabs.getTabBodyEl(currentTab);
    var edit_data = [];

    if(action == 'edit' && editFlag) {
        currentTab = tabs.getTab(e.tab_id);
        tabEl = tabs.getTabBodyEl(currentTab);
        edit_data = e.edit_data;
    }
    var param = {
        area: "div[id='" + tabEl.id + "']",
        c_id: e.id,//整个页签
        tab_id: currentTab.name//插入的当前页签
    }
    if(type == 'textbox') {
        create_textbox('tabs',edit_data, param);
    }else if(type == 'textarea') {
        create_textarea('tabs',edit_data, param);
    }else if(type == 'buttonedit') {
        create_buttonedit('tabs',edit_data, param);
    }else if(type == 'datepicker') {
        create_datepicker('tabs',edit_data, param);
    }else if(type == 'combobox') {
        create_combobox('tabs',edit_data, param);
    }else if(type == 'checkbox') {
        create_checkbox('tabs',edit_data, param);
    }else if(type == 'checkboxlist') {
        create_checkboxlist('tabs',edit_data, param);
    }
}

/**---------------------------------------操作页签 end------------------------------------------*/


/**----------------------------------------公共函数 start------------------------------------*/

//多个点击事件冲突处理
function stopBubbling(e,type) {
    e = window.event || e;
    if (e.stopPropagation) {
        if(type == 'table') {
            // e.stopPropagation();
        }else {
            // return false;
        }
        // e.stopPropagation();      //阻止冒泡但是允许默认事件的发生
        // e.preventDefault();       //阻止默认事件但是允许冒泡事件的发生
        return false                 //阻止默认事件和冒泡事件
    } else {
        e.cancelBubble = true;   //ie兼容
    }
}

//拖拽组件到主面板事件
function onMenuDrag(type) {
    console.log(type);
    mainField.ondragover = function (target) {
        target.preventDefault(); //默认是不接受被拖拽元素的,所以要禁用默认事件
    }

    mainField.ondrop = function (e) {//面板松开鼠标
        // console.log(e);
        create(type);
        // create(type,e.offsetX,e.offsetY);
    }
}
//基础组件缩放
function resize_base(id) {
    var $item = $("#" + id);
    // var num = id.substring(id.indexOf("_")+1);//截取数字部分
    $item.resizable({
        handles: 'n, e, s, w, ne, se, sw, nw',
        grid: [2, 2],
        resize: function () {
            // console.log($(this),'--');

            $('#left' + id).css({
                'line-height': $('#left' + id).height()
            });
            $('#left' + id + ' p.text').css({
                'margin-top': ($('#left' + id).height() - $('#left' + id + ' p.text').height())/2
            });
            // console.log($(this).width() - $('#left' + id).width() - 8);
            $('#right' + id).css({
                width: $(this).width() - $('#left' + id).width() - 3
            })
        }
    });
    var clickX, leftOffset;
    var dragging = false;
    var labBtn = $('#lab' + id);
    //中间线调整尺寸
    labBtn.bind('mousedown', function (e) {
        dragging = true;
        // console.log($(this).attr('id'));
        leftOffset = $('#' + id).offset().left;//左坐标
        $('#' + id).draggable('disable');
        //按下并拖动鼠标
        $('#' + id).bind('mousemove', function (e) {
            clickX = e.pageX;
            console.log(e.pageX);
            $('#left' + id).css('width', clickX - 7 - leftOffset + 'px');
            $('#lab' + id).css('left', clickX - 7 - leftOffset + 'px');
            $('#right' + id).css('left', clickX - 4 - leftOffset + 'px');
            $('#right' + id).css('width', $(this).width() - clickX + 4 + leftOffset + 'px');
        })
        e.stopPropagation();
    });

    $item.mouseup(function (e) {
        $('#' + $(this).attr('id')).unbind('mousemove');
        $('#' + $(this).attr('id')).draggable('enable');
        dragging = false;
    })
    // $item.css("position", "absolute");
    $item.draggable({
        grid: [2, 2],
        containment: "parent",
        cursor: "move",
        snap: true,
        snapTolerance: 20,
        start: function (e) {

        },
        stop: function (e) {

        }
    });

}
//miniui组件修改文字
function setBoxText(id,value) {
    var box = mini.get(id);
    box.setText(value);
}
//删除组件
function removeBox(e) {
    $("#" + e.id).remove();
    properties = properties.filter(function(x){
        if(e.id == x.c_id){
            return false
        }
        else{return true}
    })

    console.log('已删除',properties);
}

//点击组件
var click_type = '';
var click_types = [];
var click_id = '';
var click_ids = [];
var pre_click_type = '';
var pre_click_id = '';
function baseClick(e,type,id) {
    stopBubbling(e);//多个点击事件冲突处理
    cancelBlur();
    pre_click_type = click_type;//为空则为第一次点击，不存储
    pre_click_id = click_id;
    // var pre_click_id = click_ids.slice(-2, -1).toString();
    click_type = type;
    click_types.push(type);
    click_id = id;
    click_ids.push(click_id);

    showProperty(type.substring(type.indexOf('_')+1));

    console.log('--------------------------------');
    console.log('pre:',pre_click_type,',',pre_click_id);
    console.log('now:',click_type,',',click_id);
    savePre(pre_click_type,click_id,pre_click_id);
}

function savePre(pre_type,click_id,pre_id) {

    console.log('properties',properties);

    var param;
    var pre_param;
    if(click_type == 'table' || click_type == 'tab_table') {
        param = 'col_id';
    }else if(click_type == 'tab') {
        param = 'tab_id';
    }else {
        param = 'c_id';
    }
    if(pre_type == 'table' || click_type == 'tab_table') {
        // param = 'cTableField';
        pre_param = 'col_id';
    }else if(pre_type == 'tab') {
        pre_param = 'tab_id';
    }else {
        pre_param = 'c_id';
    }

    for(var i=0;i<properties.length;i++) {
        if(pre_id == properties[i][pre_param]) {
            if(pre_type == 'tab' && properties[i].type != 'tab') {
                continue;
            }
            properties[i].cTableField = cTableField.getText();//弹窗
            properties[i].cLadText = cLadText.getValue();//文本
            properties[i].cTabText = cTabText.getValue();//页签名
            properties[i].cLength = cLength.getValue();//文本
            properties[i].cFormat = cFormat.getValue();//下拉
            properties[i].cReference = cReference.getValue();//弹窗
            properties[i].cBgReference = cBgReference.getValue();//弹窗
            properties[i].cCombo = cCombo.getValue();//文本域
            properties[i].cGridType = cGridType.getValue();//下拉
            // properties[i].cMath = cMath.getValue();//公式

            properties[i].cRefObj = getForm();
        }
    }
    console.log('已存储pre',properties);

    //更新属性

    for(var i=0;i<properties.length;i++){

        if(click_id == properties[i][param]) {

            if(click_type == 'tab' && properties[i].type != 'tab') {
                continue;
            }
            var cRef = properties[i].cRefObj;
            console.log(cRef,'需要显示的属性');
            resetForm();
            if(typeof cRef == 'string') {
                cRef = $.parseJSON(cRef)
            }
            setForm(cRef);
            cMath.setText(properties[i].cMath.value);

            if(click_type == 'table' || click_type == 'tab_table') {
                //若点击的表格列,动态属性框
                setGridProperty(cGridType.getValue());
            }
        }
    }
    // cLadText.doValueChanged();

}
//表单操作
function getForm() {
    var form = new mini.Form("#form1");
    var data = form.getData();
    // var s = mini.encode(data);
    return data;
}
function setForm(obj) {
    var form = new mini.Form("#form1");
    // if(obj) {
    //     obj.cType = switchRef(obj.cType);
    // }
    form.setData(obj, false);
    cTableField.setText(obj.cTableField);
}
function resetForm() {
    var form = new mini.Form("#form1");
    form.reset();
}
function clearForm() {
    var form = new mini.Form("#form1");
    form.clear();
}

//获取主表信息
var key = '';
var key_id = '';
if(action == 'edit') {
    for(var i=0;i<edit_data.length;i++) {
        if(edit_data[i].type != 'table' && edit_data[i].type != 'tab_table' && edit_data[i].type != 'tab'){
            key = edit_data[i].cTableName;//主表名
            key_id = edit_data[i].tableClassId;//主表id
            break;
        }
    }

}
//绑定数据库表字段
function onBindKey() {
    var keySelected = [];//所有组件的字段英文名，用于判断绑定字段的唯一
    var keyList_main = [];//主表选择的表
    var keyList_child = [];//子表选择的表
    for(var i=0;i<properties.length;i++) {
        keySelected.push({
            cTableField:properties[i].cTableField,//字段
            cTableName:properties[i].cTableName//所属表
        });
        if(properties[i].type !='tab' && properties[i].type !='table' && properties[i].type !='tab_table') {
            keyList_main.push(properties[i].cTableName);
        }else if(properties[i].type =='table' || properties[i].type =='tab_table') {
            keyList_child.push(properties[i].cTableName);
        }
    }
    mini.open({
        url : bootPATH + "../frame/frame1/pages/formDesign/bindKey.html",
        title : "绑定字段",
        onload: function () {       //弹出页面加载完成
            var iframe = this.getIFrameEl();
            var data = {
                click_type: click_type,
                key: key,
                key_id: key_id,
                selected: keySelected,
                keyList_main: keyList_main,
                keyList_child: keyList_child
            };
            iframe.contentWindow.SetData(data);
        },
        ondestroy: function (action) {  //弹出页面关闭前
            if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.onTableOk();
                data = mini.clone(data);
                console.log(data,'bindKey');
                var param;
                if(click_type == 'table' || click_type == 'tab_table') {
                    param = 'col_id';
                }else {
                    key = data.tableClassName;
                    key_id = data.PID;
                    param = 'c_id';
                }
                console.log(key_id,'key_id');
                cTableField.setValue(data.field);
                cTableField.setText(data.field);
                cLadText.setValue(data.fieldCh);
                cLadText.doValueChanged();//主动激发valuechanged事件



                for(var i=0;i<properties.length;i++) {
                    if(properties[i][param] == click_id) {
                        properties[i].cTableName = data.tableClassName;//数据库表名(英文)
                        properties[i].tableClassName = data.cTableName;//数据库表名(中文)
                        properties[i].tableClassId = data.PID;//数据库表id
                    }
                }
            }
        }
    })
}

//绑定公式
function onSetMath() {

    var db = [];
    var pre;
    var param;
    if(click_type == 'table' || click_type == 'tab_table') {
        // param = 'cTableField';
        param = 'col_id';
    }else if(click_type.indexOf('tab_')>-1 && click_type != 'tab_table') {
        param = 'c_id';
    }else {
        param = 'c_id';
    }
    for(var i=0;i<properties.length;i++) {
        if(properties[i][param] == click_id) {
            db = properties[i];//点击的组件信息
            pre = properties[i].cMath;//历史公式数据
        }
    }
    mini.open({
        url : bootPATH + "../frame/frame1/pages/formDesign/setMath.html",
        title : "公式",
        style : "width:900px;height:500px;",
        onload: function () {       //弹出页面加载完成
            var iframe = this.getIFrameEl();
            var data = { pre: pre, db: db };
            iframe.contentWindow.SetData(data);
        },
        ondestroy: function (action) {  //弹出页面关闭前
            if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.onTableOk();
                data = mini.clone(data);
                console.log(data,'onSetMath');

                cMath.setText(data.value);
                cMath.setValue(data.value);

                var param;
                if(click_type == 'table' || click_type == 'tab_table') {
                    // param = 'cTableField';
                    param = 'col_id';
                }else {
                    param = 'c_id';
                }
                for(var i=0;i<properties.length;i++) {
                    if(properties[i][param] == click_id) {
                        // properties[i].cMath = data.value;
                        properties[i].cMath = data;
                    }
                }
            }
        }
    })
}

//表单取消焦点，避免input值丢失问题
function cancelBlur() {
    mini.get("cTableField").blur();
    mini.get("cLadText").blur();
    mini.get("cTabText").blur();
    mini.get("cLength").blur();
    mini.get("cReference").blur();
    mini.get("cBgReference").blur();
    mini.get("cCombo").blur();
}

//更改字段中文名
cLadText.on("valuechanged", function (e) {
    // console.log(e.value);
    // console.log(click_id,'click_id');
    var param = 'c_id';
    if(click_type == 'checkbox') {
        setBoxText(click_id.replace('box',''),e.value);

    }else if(click_type == 'table' || click_type == 'tab_table') {
        param = 'col_id';
        var grid = mini.get(c_id);
        grid.updateColumn(click_id, {
            field: cTableField.getText(),
            header: e.value,
            // name: cTableField.getText()
        });

    }else if(click_type == 'tab') {
        // param = 'tab_id';
        // //更新页签title
        // var tabs = mini.get(c_id);
        // var tab = tabs.getTab(click_id);
        // tabs.updateTab(tab, {title: e.value});

    }else {
        // $('#'+click_id+' .left p.text').html('');
        $('#'+click_id+' .left p.text').html(e.value);
    }

    if(click_type == 'tab') {

    }else {
        for(var i=0;i<properties.length;i++) {
            if(properties[i][param] == click_id) {
                properties[i].cTableField = cTableField.getText();
                properties[i].cLadText = cLadText.getValue();
                properties[i].cRefObj = getForm();
            }
        }
    }

    console.log('----pre:',pre_click_type,',',pre_click_id);
    console.log('----now:',click_type,',',click_id);
});
//更改页签名
cTabText.on("valuechanged", function (e) {
    param = 'tab_id';
    //更新页签title
    var tabs = mini.get(c_id);
    var tab = tabs.getTab(click_id);
    tabs.updateTab(tab, {title: e.value});

    console.log('----pre:',pre_click_type,',',pre_click_id);
    console.log('----now:',click_type,',',click_id);
});

//更改表格列类型 cGridType.on()
function oncGridTypeChanged(e) {
    console.log(e.value);

    var grid_id = click_id.match(/(\S*)_field/)[1];//截取

    var grid = mini.get(grid_id);
    var cols = grid.getColumns();

    //更新表格列样式
    for(var i=0;i<cols.length;i++) {
        if(cols[i].name == click_id) {
            grid.updateColumn(click_id, {editor: { type: e.value}});
        }
    }
    setGridProperty(e.value);
}

//更改复选框内容
function onComboChanged(e) {
    console.log(e.value);
    var combo_arr = e.value.split(/[(\r\n)\r\n]+/);
    console.log(combo_arr);

    var combo_data = [];
    for(var i=0;i<combo_arr.length;i++) {
        combo_data.push({
            'id':combo_arr[i],'text':combo_arr[i]
        })
    }


    if(click_type == 'table' || click_type == 'tab_table') {
        //表格设置下拉框
        mini.get(c_id).updateColumn(click_id, {editor: { type:'combobox', data: combo_data}})
    }else {
        var id = click_id.replace("combobox_","combo");
        var combo = mini.get(id);
        combo.setData(combo_data);
    }
    // var combo_data = [{
    //     'id':'1','text':'测试1'
    // },{
    //     'id':'2','text':'测试2'
    // },{
    //     'id':'3','text':'测试3'
    // }]
}

//显示/隐藏 属性栏
function showProperty(type) {

    if(type == 'textbox' || type == 'textarea') {
        $(".cLadText").css({
            display: "table-row"
        })
        $(".cTabText").css({
            display: "none"
        })
        $(".cTableField").css({
            display: "table-row"
        })
        $(".cLength").css({
            display: "table-row"
        })
        $(".cReference").css({
            display: "none"
        })
        $(".cFormat").css({
            display: "none"
        })
        $(".cCombo").css({
            display: "none"
        })
        $(".cGridType").css({
            display: "none"
        })
        $(".cMath").css({
            display: "table-row"
        })
    }else if(type == 'buttonedit') {
        $(".cLadText").css({
            display: "table-row"
        })
        $(".cTabText").css({
            display: "none"
        })
        $(".cTableField").css({
            display: "table-row"
        })
        $(".cReference").css({
            display: "table-row"
        })
        $(".cLength").css({
            display: "none"
        })
        $(".cFormat").css({
            display: "none"
        })
        $(".cCombo").css({
            display: "none"
        })
        $(".cGridType").css({
            display: "none"
        })
        $(".cMath").css({
            display: "table-row"
        })
    }else if(type == 'combobox') {
        $(".cLadText").css({
            display: "table-row"
        })
        $(".cTabText").css({
            display: "none"
        })
        $(".cTableField").css({
            display: "table-row"
        })
        $(".cCombo").css({
            display: "table-row"
        })
        $(".cReference").css({
            display: "none"
        })
        $(".cLength").css({
            display: "none"
        })
        $(".cFormat").css({
            display: "none"
        })
        $(".cGridType").css({
            display: "none"
        })
        $(".cMath").css({
            display: "table-row"
        })
    }else if(type == 'datepicker') {
        $(".cLadText").css({
            display: "table-row"
        })
        $(".cTabText").css({
            display: "none"
        })
        $(".cTableField").css({
            display: "table-row"
        })
        $(".cFormat").css({
            display: "table-row"
        })
        $(".cLength").css({
            display: "none"
        })
        $(".cReference").css({
            display: "none"
        })
        $(".cCombo").css({
            display: "none"
        })
        $(".cGridType").css({
            display: "none"
        })
        $(".cMath").css({
            display: "table-row"
        })

        dateFormat();

    }else if(type == 'checkbox') {
        $(".cLadText").css({
            display: "table-row"
        })
        $(".cTabText").css({
            display: "none"
        })
        $(".cTableField").css({
            display: "table-row"
        })
        $(".cFormat").css({
            display: "none"
        })
        $(".cLength").css({
            display: "none"
        })
        $(".cReference").css({
            display: "none"
        })
        $(".cCombo").css({
            display: "none"
        })
        $(".cGridType").css({
            display: "none"
        })
        $(".cMath").css({
            display: "none"
        })

    }else if(type == 'table') {
        $(".cLadText").css({
            display: "table-row"
        })
        $(".cTabText").css({
            display: "none"
        })
        $(".cTableField").css({
            display: "table-row"
        })
        $(".cGridType").css({
            display: "table-row"
        })
        $(".cReference").css({
            display: "none"
        })
        $(".cLength").css({
            display: "none"
        })
        $(".cFormat").css({
            display: "none"
        })
        $(".cCombo").css({
            display: "none"
        })
        $(".cMath").css({
            display: "table-row"
        })
        cGridType.setData([{
            'id':'textbox','text':'文本'
        },{
            'id':'textarea','text':'文本域'
        },{
            'id':'datepicker','text':'时间'
        },{
            'id':'combobox','text':'下拉框'
        },{
            'id':'buttonedit','text':'参照'
        }]);
    }else if(type == 'tab') {
        $(".cLadText").css({
            display: "none"
        })
        $(".cTabText").css({
            display: "table-row"
        })
        $(".cTableField").css({
            display: "none"
        })
        $(".cLength").css({
            display: "none"
        })
        $(".cReference").css({
            display: "none"
        })
        $(".cFormat").css({
            display: "none"
        })
        $(".cCombo").css({
            display: "none"
        })
        $(".cGridType").css({
            display: "none"
        })
        $(".cMath").css({
            display: "none"
        })
    }else {
        $(".cLadText").css({
            display: "none"
        })
        $(".cTabText").css({
            display: "none"
        })
        $(".cTableField").css({
            display: "none"
        })
        $(".cLength").css({
            display: "none"
        })
        $(".cReference").css({
            display: "none"
        })
        $(".cFormat").css({
            display: "none"
        })
        $(".cCombo").css({
            display: "none"
        })
        $(".cGridType").css({
            display: "none"
        })
        $(".cMath").css({
            display: "table-row"
        })
    }
}
//表格 属性栏
function setGridProperty(type) {
    if(type == 'buttonedit') {
        $(".cReference").css({
            display: "table-row"
        })

        $(".cLength").css({
            display: "none"
        })
        $(".cFormat").css({
            display: "none"
        })
        $(".cCombo").css({
            display: "none"
        })
    }else if(type == 'datepicker') {
        $(".cFormat").css({
            display: "table-row"
        })
        dateFormat();

        $(".cLength").css({
            display: "none"
        })
        $(".cReference").css({
            display: "none"
        })
        $(".cCombo").css({
            display: "none"
        })
    }else if(type == 'combobox') {
        $(".cCombo").css({
            display: "table-row"
        })
        $(".cLength").css({
            display: "none"
        })
        $(".cReference").css({
            display: "none"
        })
        $(".cFormat").css({
            display: "none"
        })
    }else if(type == 'textbox' || type == 'textarea') {
        $(".cLength").css({
            display: "table-row"
        })
        $(".cReference").css({
            display: "none"
        })
        $(".cFormat").css({
            display: "none"
        })
        $(".cCombo").css({
            display: "none"
        })
    }else {
        $(".cLength").css({
            display: "none"
        })
        $(".cReference").css({
            display: "none"
        })
        $(".cFormat").css({
            display: "none"
        })
        $(".cCombo").css({
            display: "none"
        })
    }
}
// 设置时间格式化类型
function dateFormat() {
    cFormat.setData([{
        'id':'yyyy-MM-dd','text':'yyyy-MM-dd'
    },{
        'id':'yyyy/MM/dd','text':'yyyy/MM/dd'
    }]);
}

//编辑模式时设置组件名称
function setEditBoxText(type,id) {
    var boxText = '';
    if(action == 'edit' && editFlag) {

        if(type == 'table') {

        }else if(type == 'tab'){

        }else {
            for(var i=0;i<properties.length;i++) {
                if(id == properties[i].c_id) {
                    boxText = properties[i].cLadText;
                }
            }
            if(type == 'checkbox') {
                setBoxText(id.replace('box',''), boxText);
            }else {
                $('#'+id+' .left p.text').html(boxText);
            }
        }
    }
}

//表单验证
function formValid() {
    var form1 = new mini.Form("#cFormName");
    form1.validate();
    if (form1.isValid() == false) {
        alert('表单名称不能为空');
        return;
    }
    if(!parId) {//菜单是否选中验证
        mini.alert('请选择菜单');
        return;
    }
}
//保存
function save() {
    savePre(pre_click_type,click_id,click_id);//保存最后的数据
    console.log('properties',properties);

    //存储位置参数
    var mainField = $("#mainField").children();
    console.log(mainField,'mainField');
    for(var i=0;i<mainField.length; i++){
        for(var j=0;j<properties.length;j++) {
            var c_id = properties[j].c_id;
            if(mainField[i].id == c_id) {
                properties[j].cFormName = cFormName.getValue();
                properties[j].iConX = mainField[i].offsetLeft;
                properties[j].iConY = mainField[i].offsetTop;
                properties[j].cEditCss = mainField[i].className;
                properties[j].iLabHeight = mainField[i].offsetHeight;
                properties[j].iWidth = mainField[i].offsetWidth;//总宽度
                properties[j].cMath = JSON.stringify(properties[j].cMath);
                properties[j].parId = modularId;
                properties[j].modularId= parId;
                if(properties[j].type == 'table') {
                    properties[j].iLabWidth = properties[j].iWidth;
                    var cols = CircularJSON.stringify(mini.get(properties[j].c_id).getColumns());
                    // var cols = mini.encode(mini.get(properties[j].c_id).getColumns());
                    properties[j].gridColumns = cols;
                    properties[j].lanyoutMent = '2';
                }else if(properties[j].type == 'tab') {
                    properties[j].iLabWidth = properties[j].iWidth;
                }else{
                    properties[j].iLabWidth = mainField[i].children[2].offsetWidth;//右侧宽度
                    properties[j].lanyoutMent = '1';
                }
            }
        }
    }
    //保存页签插入的组件的数据
    for(var m=0;m<properties.length;m++) {
        var c_id = properties[m].c_id;
        var tabs_id = properties[m].tabs_id;
        var tab_id = properties[m].tab_id;
        // console.log(properties[m].type.indexOf('tab_'));
        if(properties[m].type.indexOf('tab_')>-1) {//页签插入的组件
            var field = mini.get(tabs_id).getTabBodyEl(tab_id).children;
            console.log(field);
            for(var p=0;p<field.length;p++) {
                if(field[p].id == c_id) {
                    mini.get(tabs_id).activeTab(tab_id);//激活每个页签以保存数据
                    properties[m].cFormName = cFormName.getValue();
                    properties[m].iConX = field[p].offsetLeft;
                    properties[m].iConY = field[p].offsetTop;
                    properties[m].cEditCss = field[p].className;
                    properties[m].iLabHeight = field[p].offsetHeight;
                    properties[m].iWidth = field[p].offsetWidth;//总宽度
                    properties[m].cMath = JSON.stringify(properties[m].cMath);
                    properties[m].parId = modularId;
                    properties[m].modularId= parId;
                    if(properties[m].type == 'tab_table') {
                        var cols = CircularJSON.stringify(mini.get(properties[m].c_id).getColumns());
                        properties[m].gridColumns = cols;
                        properties[m].iLabWidth = properties[m].iWidth;
                        properties[m].lanyoutMent = '2';
                    }else {
                        properties[m].iLabWidth = field[p].children[2].offsetWidth;//右侧宽度
                        properties[m].lanyoutMent = '1';
                    }
                }
            }
        }
    }
    console.log(properties,'已保存');
    console.log(JSON.stringify(properties));

    // formValid();
    // 表单验证
    var form1 = new mini.Form("#cFormName");
    form1.validate();
    if (form1.isValid() == false) {
        mini.alert('表单名称不能为空');
        return;
    }
    if(!parId) {//菜单是否选中验证
        mini.alert('请选择菜单');
        return;
    }

    //传数据
    var url;

    if(action == 'edit') {
        url =  ajaxUrl + "/formltems/updateForm.do";
    }else {
        url =  ajaxUrl + "/formltems/insertForm.do";
    }

    $.ajax({
        type : 'POST',
        url : url,
        dataType : 'json',
        contentType: "application/json",
        data : JSON.stringify(properties),
        success: function (data) {
            mini.alert('保存成功！');
            // parent.removeTab();//保存成功，关闭页面
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert('保存失败！');
            // console.log(jqXHR);
        }
    })
}

// move(textbox,mainField,'textbox');
//
// function move(ele, box, type) {
//     // box.ondragover = function (target) {
//     //     target.preventDefault(); //默认是不接受被拖拽元素的,所以要禁用默认事件
//     // }
//     // ele.onmousedown = function () {//按下组件
//     //     // box.ondrop = function () {//面板松开鼠标
//     //     //     // console.log(dragType,'拖动类型')
//     //     //     create(type);
//     //     // }
//     // }
//
//     ele.onmousedown = function () {//选中组件并移动
//         console.log('移动元素');
//         // 当被拖拽的元素经过时触发
//         box.ondragover = function (target) {
//             target.preventDefault(); //默认是不接受被拖拽元素的,所以要禁用默认事件
//         }
//
//         box.ondrop = function () {//面板松开鼠标
//             // console.log(dragType,'拖动类型')
//             create(type);
//         }
//     }
// }

//对齐方式
function Alignment(value) {
    var mainField = $("#mainField").children();
    // console.log(mainField,'mainField');
    for(var i = 0; i < mainField.length; i++){
        switch (value) {
            case 'left':
                $("#"+ mainField[i].id).css({ "left": "0px", "float": "none"});
                break;
            case 'right':
                $("#"+ mainField[i].id).css({ "left": "auto", "right": "0px", "float": "right"});
                break;
            case 'bottom':
                $("#"+ mainField[i].id).css({ "top": "auto", "bottom": "0px", "position": "absolute", "float": "none"});
                break;
            case 'top':
                $("#"+ mainField[i].id).css({ "top": "0px", "float": "none"});
                break;
        }

    }
}

//选择所属菜单
var modularId;
var parId;
function setMenu() {
    mini.open({
        url : bootPATH + "../frame/frame1/pages/formLayout/formDesign_selectMenu.html",
        title : "选择所属菜单",
        onload: function () {       //弹出页面加载完成
            var iframe = this.getIFrameEl();
            var data = { action: action, modularId: modularId, parId: parId };
            iframe.contentWindow.SetMenuData(data);
        },
        ondestroy: function (action) {  //弹出页面关闭前
            if (action == "ok") {       //如果点击“确定”
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.onTableOk();
                data = mini.clone(data);
                console.log(data,'----');
                // modularId = data[0].id.slice(6);//节点id
                // parId = data[0].pid.slice(6);//父节点id
                modularId = data[0].id.replace(/[^0-9]/ig,"");//节点id
                parId = data[0].pid.replace(/[^0-9]/ig,"");//父节点id
                console.log(modularId,'modularId');
                console.log(parId,'parId');
            }
        }
    })
}
/**----------------------------------------公共函数 end--------------------------------------*/
